package com.coin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.TradeArea;
import com.coin.model.R;
import com.coin.service.TradeAreaService;
import com.coin.vo.TradeAreaMarketVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @Auther: 李 力
 * @Date: 2025/6/20
 * @Description: com.coin.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/tradeAreas") //tradeAreas
@Api(tags = "交易区域的数据接口")
public class TradeAreaController {

    @Autowired
    private TradeAreaService tradeAreaService;

    /**
     * @Description: 交易区域的分页查询
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @GetMapping
    @ApiOperation(value = "交易区域的分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
            @ApiImplicitParam(name = "name", value = "交易区域的名称"),
            @ApiImplicitParam(name = "status", value = "交易区域的状态"),
    })
    @PreAuthorize("hasAuthority('trade_area_query')")
    public R<Page<TradeArea>> findByPage(@ApiIgnore Page<TradeArea> page, String name, Byte status) {
        Page<TradeArea> pageData = tradeAreaService.findByPage(page, name, status);
        return R.ok(pageData);
    }


    /**
     * @Description: 新增一个交易区域
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @PostMapping
    @ApiOperation(value = "新增一个交易区域")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeArea", value = "tradeAreajson")
    })
    @PreAuthorize("hasAuthority('trade_area_create')")
    public R save(@RequestBody TradeArea tradeArea) {
        boolean isOk = tradeAreaService.saveTradeArea(tradeArea);
        return isOk ? R.ok() : R.fail();
    }

    /**
     * @Description: 修改一个交易区域
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @PutMapping
    @ApiOperation(value = "修改一个交易区域")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeArea", value = "tradeAreajson")
    })
    @PreAuthorize("hasAuthority('trade_area_update')")
    public R update(@RequestBody TradeArea tradeArea) {
        boolean isOk = tradeAreaService.updateTradeArea(tradeArea);
        return isOk ? R.ok() : R.fail();
    }

    /**
     * @Description: 修改一个交易区域的状态
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @PutMapping("/status")
    @ApiOperation(value = "修改一个交易区域的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeArea", value = "tradeAreajson")
    })
    @PreAuthorize("hasAuthority('trade_area_update')")
    public R updateStatus(@RequestBody TradeArea tradeArea) {
        boolean isOk = tradeAreaService.updateStatus(tradeArea);
        return isOk ? R.ok() : R.fail();
    }

    /**
     * @Description: 根据状态查询交易区域
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @GetMapping("/all")
    @ApiOperation(value = "查询交易区域")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "交易区域的状态")
    })
    @PreAuthorize("hasAuthority('trade_area_query')")
    public R<List<TradeArea>> findAll(Byte status) {
        List<TradeArea> tradeAreas = tradeAreaService.findAll(status);
        return R.ok(tradeAreas);
    }

    /**
     * @Description: 交易区域查询
     * @Author: 李 力
     * @Date: 2025/6/21
     * @Param:
     * @return: R
     */
    @GetMapping("/markets")
    public R<List<TradeAreaMarketVo>> listR() {
        List<TradeAreaMarketVo> voList = tradeAreaService.findTradeArea();
        return R.ok(voList);
    }

    /**
     * @Description: 用户收藏的交易市场查询
     * @Author: 李 力
     * @Date: 2025/6/23
     * @Param:
     * @return: R
     */
    @GetMapping("/market/favorite")
    public R<List<TradeAreaMarketVo>> userSC() {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        List<TradeAreaMarketVo> tradeAreaMarketVos = tradeAreaService.getUserFavoriteMarkets(userId);
        return R.ok(tradeAreaMarketVos);
    }
}
